/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "DbLoader.h"

#include <QtCore/QFile>
#include <QtCore/QTime>

#include "SeqDatabase.h"
#include "SqlServer.h"
#include "GenBankParser.h"
#include "GzFile.h"

/**
**/
DbLoader::DbLoader()
{
	registerArg( "seqdb", m_seqdb, true );
}

/**
**/
DbLoader::~DbLoader()
{
}

/**
**/
bool DbLoader::run( Bio::SqlServer &server )
{
	bool				status = true;
	Bio::SeqDatabase	seqdb = server.getSeqDatabase( m_seqdb );
	QStringListIterator	fileIt( getGeneralArgs() );
	QTime				timer;

	output( "Loading Files..." );

	timer.start();

	while( fileIt.hasNext() ){
		if( !loadFile( fileIt.next(), seqdb ) ){
			status = false;
			break;
		}
	}

	int	elapsed = timer.elapsed() / 100;

	output( QString( "Finished in %1 min %2.%3 sec" ).arg( elapsed / 600 )
		.arg( (elapsed % 600) / 10 ).arg( elapsed % 10 ) );

	return status;
}

/**
**/
void DbLoader::printUsage()
{
	output( "Usage:\n" );
	output( "\tBioSQLLoader filename [filename] [...]" );
}

/**
**/
bool DbLoader::loadFile( const QString &filename, Bio::SeqDatabase &db )
{
	bool				status = true;
	Bio::GenBankParser	parser;
	QTime				timer;
	int					elapsed;

	output( QString( "   Parsing File %1..." ).arg( filename ) );

	timer.start();

	if( filename.right( 3 ).toLower() == ".gz" ){
		Bio::GzFile	file( filename );
		if( !file.open( QIODevice::ReadOnly ) ){
			error( QString( "Could not open file '%1' for reading." ).arg( filename ) );
			return false;
		}
		status = parser.parse( file );
		file.close();
	}
	else {
		QFile	file( filename );
		if( !file.open( QIODevice::ReadOnly ) ){
			error( QString( "Could not open file '%1' for reading." ).arg( filename ) );
			return false;
		}
		status = parser.parse( file );
		file.close();
	}

	elapsed = timer.elapsed() / 100;
	output( QString( "   File read in %1 min %2.%3 sec" ).arg( elapsed / 600 )
		.arg( (elapsed % 600) / 10 ).arg( elapsed % 10 ) );

	if( !status ){
		error( "Failed to parse records from file." );
		return false;
	}

	timer.restart();

	status = db.load( &parser );
	if( !status )
		error( "Failed to load records into database." );

	elapsed = timer.elapsed() / 100;
	output( QString( "   Records loaded in %1 min %2.%3 sec" ).arg( elapsed / 600 )
		.arg( (elapsed % 600) / 10 ).arg( elapsed % 10 ) );

	return status;
}
